package queries;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import connect.DBConnectionUtil;

public class Query5 extends JFrame{
	public Query5(){
		this.setBounds(300,240,600,400);
		this.setLayout(null);
		JLabel label0 = new JLabel("Enter patient's info and test result");
		label0.setBounds(200, 10, 500, 30);
		this.add(label0);
		JLabel label1 = new JLabel("Pid:");
		JTextField pid = new JTextField();
		label1.setBounds(100, 50, 100, 30);
		pid.setBounds(200, 50, 200, 30);
		this.add(label1);
		this.add(pid);
		JLabel label2 = new JLabel("Tname");
		JTextField tname = new JTextField();
		label2.setBounds(100, 100, 100, 30);
		tname.setBounds(200, 100, 200, 30);
		this.add(label2);
		this.add(tname);
		JLabel label3 = new JLabel("Date");
		JTextField date = new JTextField();
		JLabel label33 = new JLabel("(yyyy-mm-dd hh24:mi)");
		label3.setBounds(100, 150, 100, 30);
		date.setBounds(200, 150, 200, 30);
		label33.setBounds(410, 150, 200, 30);
		this.add(label3);
		this.add(date);
		this.add(label33);
		
		JLabel label4 = new JLabel("Result");
		JTextField result = new JTextField();
		label4.setBounds(100, 200, 100, 30);
		result.setBounds(200, 200, 200, 30);
		this.add(label4);
		this.add(result);
		
		JButton insert = new JButton("Insert");
		insert.setBounds(200, 250, 100, 30);
		this.add(insert);
		this.setTitle("Query5:Enter patient's info and result");
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setVisible(true);
		insert.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					// 1.connect database
					Class.forName(DBConnectionUtil.DBDRIVER).newInstance();
					Connection con = DBConnectionUtil.getConnection();
					if(con!=null){
						System.out.println("Connect database successfully!");
					}
					PreparedStatement ps = con
							.prepareStatement("SELECT  * FROM Patient WHERE pid=?");
					ps.setInt(1, Integer.parseInt(pid.getText()));
					ResultSet rset = ps.executeQuery();
					if (!rset.next()) {
						JOptionPane.showMessageDialog(null,
								"Pid does not exsit!");
						return;
					}
					ps = con.prepareStatement("SELECT  * FROM Test WHERE tname=?");
					ps.setString(1, tname.getText());
					rset = ps.executeQuery();
					if (!rset.next()) {
						JOptionPane.showMessageDialog(null,
								"Tname does not exsit!");
						return;
					}
					// 2.query code
					 ps = con.prepareStatement("INSERT INTO Test (tdate, tname) VALUES (TO_DATE(?, 'yyyy-mm-dd hh24:mi'), ?)");
					ps.setString(1, date.getText());
					ps.setString(2, tname.getText());
					System.out.println(date.getText());
					ps.executeQuery();
					ps.close();
					
					ps = con.prepareStatement("INSERT INTO Takes (pid, tname, result) VALUES (?,?,?)");
					ps.setString(1, pid.getText());
					ps.setString(2, tname.getText());
					ps.setString(3, result.getText());
					ps.executeQuery();
					ps.close();
					

					JOptionPane.showMessageDialog(null, "Insert Takes Info Successfully!");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
		});
	}
}
